package com.example.app.moudle.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ColorUtils
import com.example.app.R
import com.example.app.base.BaseSupportFragment
import com.example.app.databinding.FHomeBinding
import com.example.app.moudle.home.adapter.HomeMultiAdapter
import com.example.app.moudle.home.entity.HomeTabEntity
import com.example.app.moudle.home.vm.HomeVM
import com.google.android.material.tabs.TabLayout

/**
 * 普通页面模板
 *
 * 常规 启动:start 关闭：pop  关闭到栈：popTo 启动并关闭到startWithPopTo
 * 具体方法可以查看 SupportFragment 提供的相关方法
 */
class HomeF : BaseSupportFragment<FHomeBinding>() {
    override fun getLogTag() = "HomeF"
    override fun createViewBinding(inflater: LayoutInflater) = FHomeBinding.inflate(inflater)

    companion object {
        fun newInstance(): HomeF {
            return HomeF()
        }
    }

    private val tabList = mutableListOf<HomeTabEntity>()
    private val vpAdapter by lazy { HomeMultiAdapter(this@HomeF) }
    var currentPage = HomeTabEntity.TAB_RECOMMEND

    /**
     * tab选中监听
     */
    private val tabSelectListener = object : TabLayout.OnTabSelectedListener {
        override fun onTabSelected(tab: TabLayout.Tab?) {
            tab?.let {
                it.customView?.apply {
                    findViewById<TextView>(R.id.tvTabName)?.setTextColor(ColorUtils.getColor(R.color.color_white))
                    findViewById<View>(R.id.viewIndicate)?.isVisible = true
                }
                mViewBinding?.vpContent?.setCurrentItem(it.position, false)
            }
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
            tab?.customView?.apply {
                findViewById<TextView>(R.id.tvTabName)?.setTextColor(ColorUtils.getColor(R.color.white_translucent_50))
                findViewById<View>(R.id.viewIndicate)?.isVisible = false
            }
        }

        override fun onTabReselected(tab: TabLayout.Tab?) {}
    }

    /**
     * viewpager2选中监听
     */
    private val onPagerChangeListener = object : ViewPager2.OnPageChangeCallback() {

        override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            mViewBinding?.tabLayout?.setScrollPosition(position, positionOffset, true, true)
        }

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            currentPage = position
            mViewBinding?.tabLayout?.let { it.selectTab(it.getTabAt(position)) }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initTabLayout()
        initViewPager()
    }

    /**
     * 初始化tabLayout
     */
    private fun initTabLayout() {
        mViewBinding?.apply {
            tabList.clear()

            tabList.add(HomeTabEntity(HomeTabEntity.TAB_FOLLOW, "Follow"))
            tabList.add(HomeTabEntity(HomeTabEntity.TAB_RECOMMEND, "Recommend"))
            tabList.add(HomeTabEntity(HomeTabEntity.TAB_SPECIAL, "Special"))

            tabLayout.removeAllTabs()
            tabList.forEachIndexed { index, tabData ->
                val tab = tabLayout.newTab()

                @SuppressLint("InflateParams")
                val tabCustomView = LayoutInflater.from(context).inflate(R.layout.layout_tab_home, null)
                tabCustomView?.findViewById<TextView>(R.id.tvTabName)?.apply {
                    setTextColor(ColorUtils.getColor(if (index == 0) R.color.color_white else R.color.white_translucent_50))
                    text = tabData.tabName
                }
                tabCustomView?.findViewById<View>(R.id.viewIndicate)?.isVisible = if (index == 0) tabList.size > 1 else false
                tab.customView = tabCustomView
                tab.tag = index
                tabLayout.addTab(tab)
            }
            tabLayout.addOnTabSelectedListener(tabSelectListener)
        }
    }

    /**
     * 初始化viewpager2
     */
    private fun initViewPager() {
        mViewBinding?.vpContent?.apply {
            orientation = ViewPager2.ORIENTATION_HORIZONTAL
            registerOnPageChangeCallback(onPagerChangeListener)
            adapter = vpAdapter

            offscreenPageLimit = 3

            vpAdapter.submitList(tabList)
            post { setCurrentItem(HomeTabEntity.TAB_RECOMMEND, false) }


        }
    }

}